﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using System.Data;
using System.Data.SqlClient;

public partial class member_report : System.Web.UI.Page
{
	public string EnterpriseName = "";
    public string UserID = "";
    public string UserPower = "";
    public string Footer = "";
	
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Init.islogin(ref UserID,ref EnterpriseName,ref UserPower))
        {
            Response.Redirect("/Login.aspx");
        }        
        if (!UserPower.Contains("Manager"))
        {
            Response.Write("你没有权限！");
            Response.End();
        }
        //this.EnableViewState = false;
        //id = Request.Params["SID"];
        fstate = Request.Params["fstate"];
        djbh = Request.Params["djbh"];
        pihao = Request.Params["pihao"];
        ghs = Request.Params["ghs"];
        crksrq = Request.Params["crksrq"];
        crkerq = Request.Params["crkerq"];
        keyword = Request.Params["keyword"];
        /*
        if (!string.IsNullOrEmpty(fstate))
        {
            ddlfstate.SelectedValue = fstate;
        }
         */
        if (!string.IsNullOrEmpty(djbh) && string.IsNullOrEmpty(dj.Text))
        {
            dj.Text = djbh;
        }
        if (!string.IsNullOrEmpty(pihao) && string.IsNullOrEmpty(ph.Text))
        {
            ph.Text = pihao;
        }
        if (!string.IsNullOrEmpty(ghs) && string.IsNullOrEmpty(gh.Text))
        {
            gh.Text = ghs;
        }
        if (!string.IsNullOrEmpty(crksrq) && string.IsNullOrEmpty(srq.Text))
        {
            srq.Text = crksrq;
        }
        if (!string.IsNullOrEmpty(crkerq) && string.IsNullOrEmpty(erq.Text))
        {
            erq.Text = crkerq;
        }
        if (!string.IsNullOrEmpty(keyword) && string.IsNullOrEmpty(k.Text))
        {
            k.Text = keyword;
        }
        Response.Write(Init.InitDefault());
        DataTable dt = mssql.ExecuteDt("select '全部' as fstate,0 as xlh union select '未上传' as fstate,1 as xlh union  select '已上传' as fstate,2 as xlh order by xlh");
        if (!IsPostBack)
        {
            ddlfstate.DataSource = dt;
            ddlfstate.DataTextField = ddlfstate.DataValueField = "fstate";
            ddlfstate.DataBind();
            ddlfstate.SelectedValue = fstate;     
             DataGroup(); 
        }
    }
    Common Init = new Common();
    MsSqlHelper mssql = new MsSqlHelper();
    public int recordCount = 0;

    public string fstate = "";
    public string djbh = "";
    public string pihao = "";
    public string ghs = "";
    public string crksrq = "";
    public string crkerq = "";
    public string keyword = "";
    //public string srq = "";

    //分页
    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GridView1.PageIndex = e.NewPageIndex;
        DataGroup();

    }

    protected void Submit_Click(object sender, EventArgs e)
    {
        DataGroup();
    }

    //展示
    public void DataGroup()
    {
        djbh = dj.Text;
        pihao = ph.Text;
        ghs = gh.Text;
        crksrq = srq.Text;
        crkerq = erq.Text;
        keyword = k.Text;
        fstate = ddlfstate.SelectedItem.Text;

        string sql = "";
        try
        {
            sql += "select top 1600 "
                +" max(c.rq) as rq,rtrim(b.bh) as bh,b.spid,rtrim(b.spbh) as spbh,rtrim(b.spmch) as spmch,rtrim(b.shpgg) as shpgg,rtrim(b.dw) as dw,rtrim(b.shengccj) as shengccj,c.pihao,'' url "
                + " from spkfk b(nolock) ";
            sql += (!string.IsNullOrEmpty(djbh) && djbh.IndexOf("XSA") > -1) ? " left join spls_ck c on b.spid=c.spid and c.djbh like 'XSA%' " : " left join spls_rk c(nolock) on b.spid=c.spid and c.djbh like 'JHA%' ";
            sql += (!string.IsNullOrEmpty(ghs)) ?" left join mchk d(nolock) on c.dwbh=d.dwbh ":"";
            sql += " left join [ImageDoc] a(nolock) on a.detailid=b.spid and a.beactive='是' and a.type_id='bg' and replace(rtrim(a.fzxx1),' ','')=replace(rtrim(c.pihao),' ','') ";
            sql += (!string.IsNullOrEmpty(djbh)) ? " and a.fzxx1=c.pihao " : "";            
            sql += " where b.beactive='是' and b.leibie!='赠品' and isnull(c.pihao,'')!='' ";
            sql += (fstate == "未上传") ?" and isnull(a.imageid,'')=''":"";
            sql += (fstate == "已上传") ? " and isnull(a.imageid,'')!=''" : "";
            sql += (!string.IsNullOrEmpty(ghs)) ? " and (d.danwbh like '%" + ghs + "%' or d.dwmch like '%" + ghs + "%' or d.zjm like '%" + ghs + "%') " : "";
            sql += (!string.IsNullOrEmpty(djbh)) ? " and c.djbh='" + djbh + "' " : "";
            sql += (!string.IsNullOrEmpty(crksrq) && string.IsNullOrEmpty(djbh)) ? " and c.rq>='" + crkerq + "' " : " and c.rq>= convert(varchar(10),getdate()-3,120) ";
            sql += (!string.IsNullOrEmpty(crkerq) && string.IsNullOrEmpty(djbh)) ? " and c.rq<='" + crkerq + "' " : " and c.rq<= convert(varchar(10),getdate(),120) ";
            sql += (!string.IsNullOrEmpty(pihao)) ? " and c.pihao like'" + pihao + "%' " : "";
            sql += (!string.IsNullOrEmpty(keyword)) ? " and (b.spid like'" + keyword + "%' or b.spbh like'" + keyword + "%' or b.spmch like'" + keyword + "%' or b.zjm like'" + keyword + "%')" : "";            
            sql += " group by b.bh,b.spid,b.spbh,b.spmch,b.shpgg,b.dw,b.shengccj,c.pihao ";
            sql += " order by max(c.rq) desc";
            if (Init.IsDebug)
            {
                ResultDeBug.Text = sql;
            }
            DataSet dss = mssql.ExecuteDs(sql);
            for (int i = 0; i < dss.Tables[0].Rows.Count; i++)
            {
                dss.Tables[0].Rows[i]["url"] = herf(dss.Tables[0].Rows[i]["spid"].ToString(), dss.Tables[0].Rows[i]["pihao"].ToString(),"bg");
            }
            recordCount = dss.Tables[0].Rows.Count;
            GridView1.DataSource = dss;
            GridView1.DataKeyNames = new string[] { "spid" };//主键       
            GridView1.DataBind();
        }
        catch (Exception ex)
        {
            msg.Text = "<br /><br /><br /><br />" + ex.Message;
        }
    }
    public string herf(string id,string batch_number, string ret)
    {
        DataSet dsmx = new DataSet();
        dsmx.Clear();
        string detailid = id;
        string retsult = "";
        string sql = "select rtrim(imageid) as imageid,rtrim(url) as url from [ImageDoc](nolock) where type_id=rtrim('" + ret + "') and detailid=rtrim('" + detailid + "') and replace(rtrim(fzxx1),' ','')=replace(rtrim('" + batch_number + "'),' ','')";
        dsmx = mssql.ExecuteDs(sql);
        retsult = retsult + "<!--" + sql + "-->\r\n";

        if (dsmx.Tables[0].Rows.Count == 0)
        {
            retsult = retsult + " <a class=\"upload\" href=\"javascript:;\" data-type=\"file\" data-dap=\"" + ret + "\" data-id=\"" + id + "\" data-batch=\"" + batch_number + "\" style=\"color: #666; border: 1px solid #d2ccc4;display: inline-block;margin: 5px 0;\">上传</a> \r\n";
        }
        else
        {

            retsult = retsult + " <a class=\"upload\" href=\"javascript:;\" data-type=\"file\" data-dap=\"" + ret + "\" data-id=\"" + id + "\" data-batch=\"" + batch_number + "\" style=\"color: #ea394c;border: 1px solid #d2ccc4;display: inline-block;margin: 5px 0;\">上传</a> \r\n";
			int rowid=0;
            for (int i = 0; i < dsmx.Tables[0].Rows.Count; i++)
            {
                string fileurl = Init.imagefileurl(dsmx.Tables[0].Rows[i]["url"].ToString());
                if (!string.IsNullOrEmpty(fileurl))
                {
                    //retsult += " <a class=\"btn-view preview\" href=\"" + fileurl + "\" rel=\"" + dsmx.Tables[0].Rows[i]["rel"].ToString() + "\" " + (rowid == 0 ?"": " style=\"display:none;\"") + ">预览</a>\r\n";
                    retsult = retsult + " <a href=\"" + dsmx.Tables[0].Rows[i]["url"].ToString() + "\"  data-img=\"" + dsmx.Tables[0].Rows[i]["imageid"].ToString() + "\" rel=\"" + id + "|" + batch_number + "\"  src=\"" + dsmx.Tables[0].Rows[i]["url"].ToString() + "\" data-dap=\"" + ret + "\"  class=\"view\" " + (rowid == 0 ? "" : " style=\"display:none;\"") + "  style=\"color: #ea394c;border: 1px solid #d2ccc4;display: inline-block;margin: 5px 0;\">预览</a>\r\n";
					rowid++;
                }
                /*
                if (i == 0)
                {
                    retsult = retsult + " <a href=\"" + dsmx.Tables[0].Rows[i]["url"].ToString() + "\"  data-img=\"" + dsmx.Tables[0].Rows[i]["imageid"].ToString() + "\" rel=\"" + id + "|" + batch_number + "\"  src=\"" + dsmx.Tables[0].Rows[i]["url"].ToString() + "\" data-dap=\"" + ret + "\"  class=\"view\"  style=\"color: #ea394c;border: 1px solid #d2ccc4;display: inline-block;margin: 5px 0;\">预览</a>\r\n";
                }
                else
                {
                    retsult = retsult + " <a href=\"" + dsmx.Tables[0].Rows[i]["url"].ToString() + "\"  data-img=\"" + dsmx.Tables[0].Rows[i]["imageid"].ToString() + "\" rel=\"" + id + "|" + batch_number + "\"  src=\"" + dsmx.Tables[0].Rows[i]["url"].ToString() + "\" data-dap=\"" + ret + "\"  class=\"view\" style=\"display:none;\"  style=\"color: #ea394c;border: 1px solid #d2ccc4;display: inline-block;margin: 5px 0;\">预览</a>\r\n";
                }
                 */
            }
        }
        return retsult;
    }
}